module gui.msgbox;

import mirdefine, mirshare, mirgame, mirsound;
import gui.gwindow;
import common.imagearray;
import share.utility;

import AGui.AGui;
import AGui.Widgets.Frame.Frame;
import AGui.Widgets.Button.Button;
import AGui.Widgets.Label.Label;
import AGui.Widgets.TextField.TextField;

import AGui.Backends.Allegro5.Allegro5;

import allegro5.allegro;
import allegro5.allegro_image;


public enum MsgBoxBtnType
{
    Yes = 1,
    No = 2,
    Ok = 4,
    Cancel = 8,
    EditCtrl = 256
}

const mrOk = 1;
const mrYes = 2;
const mrCancel = 4;
const mrNo = 8;


public class MessageBox : GWindow, ActionListener
{
    private Button DMsgDlgOk;
    private Button DMsgDlgYes;
    private Button DMsgDlgCancel;
    private Button DMsgDlgNo;
    private Label DMsgLabel;
    private TextField DInput;

    private int DialogResult;
    private string msgText;
    private bool _got_focus;



    public void actionPerformed(ActionEvent evt)
    {
        Button button = cast(Button)evt.getSource();
        if (button !is null)
        {
            doButtonClicked(button);
        }
    }

    public this()
    {
        super();

        this.DialogResult = -1;
        this.msgText = this.msgText.init;
        this._got_focus = false;
        
        //initialize();
    }
    
    public ~this()
    {
        //this.
        delete DMsgDlgOk;
        delete DMsgDlgCancel;
        delete DMsgDlgYes;
        delete DMsgDlgNo;
        delete DMsgLabel;
        delete DInput;
    }

    public void createWindow(ImageArray iarr)
    {
        super.createWindow(iarr, _WNDIMGINDX_MSGBOX, true, 0, 0);
    }
    
    public void initialize()
    {
        immutable btnCol = Color(100, 100, 100, 255);

        DMsgDlgOk = new Button;
        DMsgDlgOk.setBackColor(btnCol);
        DMsgDlgOk.setSize(80, 20);
        DMsgDlgOk.setLocation(0, 195);
        DMsgDlgOk.setVisibility(false);
        DMsgDlgOk.setText("Ok");
        DMsgDlgOk.addActionListener(this);
        
        DMsgDlgCancel = new Button;
        DMsgDlgCancel.setBackColor(btnCol);
        DMsgDlgCancel.setSize(80, 20);
        DMsgDlgCancel.setLocation(0, 195);
        DMsgDlgCancel.setVisibility(false);
        DMsgDlgCancel.setText("Cancel");
        DMsgDlgCancel.addActionListener(this);
        
        DMsgDlgYes = new Button;
        DMsgDlgYes.setBackColor(btnCol);
        DMsgDlgYes.setSize(80, 20);
        DMsgDlgYes.setLocation(0, 195);
        DMsgDlgYes.setVisibility(false);
        DMsgDlgYes.setText("Yes");
        DMsgDlgYes.addActionListener(this);
        
        DMsgDlgNo = new Button;
        DMsgDlgNo.setBackColor(btnCol);
        DMsgDlgNo.setSize(80, 20);
        DMsgDlgNo.setLocation(0, 195);
        DMsgDlgNo.setVisibility(false);
        DMsgDlgNo.setText("No");
        DMsgDlgNo.addActionListener(this);
        
        DMsgLabel = new Label;
        DMsgLabel.setFontColor(Color(1f, 1f, 1f, 1f));
        DMsgLabel.setAutosizing(false);
        DMsgLabel.setSingleLine(false);
        DMsgLabel.setLocation(25, 85);
        DMsgLabel.setSize(315, 100);
        
        DInput = new TextField;
        //DInput.setBackColor(btnCol);
        DInput.setSize(313, 22);
        DInput.setLocation(22, 227);
        DInput.setMaxLength(20);
        DInput.setVisibility(false);

        this.setVisibility(false);
        this.setMovable(false);
        //this.setText("Login");
        //this.setFrontColor(dlgCol);
        //this.setSize(300, 200);
        //this.setLocation(_LEFT_LOGIN, _TOP_LOGIN);
        
        this.add(DMsgDlgOk);
        this.add(DMsgDlgCancel);
        this.add(DMsgDlgYes);
        this.add(DMsgDlgNo);
        this.add(DMsgLabel);
        this.add(DInput);
        
        guiMan.add(this);
    }
    
    public void show(string msgstr, MsgBoxBtnType buttons)
    {
        const XBase = 260;
        
        int lx = XBase, ly = 195;
        
        msgText = msgstr;
        DMsgLabel.setText(msgstr);
        DMsgDlgOk.setVisibility(false);
        DMsgDlgYes.setVisibility(false);
        DMsgDlgCancel.setVisibility(false);
        DMsgDlgNo.setVisibility(false);
        DInput.setVisibility(false);
        
        this.setLocation((SCREENWIDTH - this.getWidth()) / 2, (SCREENHEIGHT - this.getHeight()) / 2);
        
        
        if (buttons & MsgBoxBtnType.Cancel)
        {
            DMsgDlgCancel.setLocation(lx, ly);
            DMsgDlgCancel.setVisibility(true);
            lx = lx - 85;
        }
        if (buttons & MsgBoxBtnType.No)
        {
            DMsgDlgNo.setLocation(lx, ly);
            DMsgDlgNo.setVisibility(true);
            lx = lx - 85;
        }
        if (buttons & MsgBoxBtnType.Yes)
        {
            DMsgDlgYes.setLocation(lx, ly);
            DMsgDlgYes.setVisibility(true);
            lx = lx - 85;
        }
        if ((buttons & MsgBoxBtnType.Ok) || (lx == XBase))
        {
            DMsgDlgOk.setLocation(lx, ly);
            DMsgDlgOk.setVisibility(true);
            lx = lx - 85;
        }
        
        if (buttons & MsgBoxBtnType.EditCtrl)
        {
            DInput.setVisibility(true);
        }

        //this.getGui().

        this.bringToFront();
        this._got_focus = this.requestModalFocus();
        super.show();
    }
    
    public void doButtonClicked(Button button)
    {
        SoundMan.PlaySound(s_norm_button_click);

        if (button is DMsgDlgOk) this.DialogResult = mrOk;
        if (button is DMsgDlgYes) this.DialogResult = mrYes;
        if (button is DMsgDlgCancel) this.DialogResult = mrCancel;
        if (button is DMsgDlgNo) this.DialogResult = mrNo;

        if (this._got_focus)
            this._got_focus = this.releaseModalFocus();
        this.hide();
    }
    
    /+private void renderMessage()
    {
        const msglx = 39 + 5;
        const msgly = 38 + 5;
        int ly, px, py, i;
        string str, data;
        
        ly = msgly;
        str = MsgText;
        while (true)
        {
            if (str == "") break;
            str = GetValidStr3(str, data, ['\\']);
            if (data != "")
                MyGame.drawShadowText(SurfaceX(Left + msglx), SurfaceY(Top + ly), AlWhite, AlBlack, data);
            ly = ly + 14;
        }
    }


    private void DMsgDlgKeyDown(DControl sender, KeyboardEventArgs kb)
    {
        if (kb.KeyCode == ALLEGRO_KEY_ENTER)
        {
            //2003/02/11 OK/Cancel¿¡¼­´Â ¿£ÅÍ¸¦ OK·Î...
            if (DMsgDlgOk.visible && !(DMsgDlgYes.visible /*|| DMsgDlgCancel.visible*/ || DMsgDlgNo.visible))
            {
                this.DialogResult = mrOk;
                this.visible = false;
            }
            if (DMsgDlgYes.visible && !(DMsgDlgOk.visible || DMsgDlgCancel.visible))
            {
                this.DialogResult = mrYes;
                this.visible = false;
            }
        }
        
        if (kb.KeyCode == ALLEGRO_KEY_ESCAPE)
        {
            if (DMsgDlgNo.visible)
            {
                this.DialogResult = mrNo;
                this.visible = false;
            }
            if (DMsgDlgCancel.visible)
            {
                this.DialogResult = mrCancel;
                this.visible = false;
            }
        }
    }
    +/
}